package com.mengxuegu.member.filter;

import com.mengxuegu.member.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
 * 登录认证拦截器
 * */
@Component
public class AuthenticationFilter extends HandlerInterceptorAdapter {

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //请求头带上令牌 Authorization: Bearer jwtToken
        final String authHeader = request.getHeader("Authorization");
        if (StringUtils.isNotBlank(authHeader) && authHeader.startsWith("Bearer ")){
            //截取token
            final String token = authHeader.substring(7);
            //解析token
            Claims claims = jwtUtil.parseJWT(token);
            if (claims != null){
                //是否登录
                Boolean b = (Boolean)claims.get("isLogin");
                if (b) {
                    //已经登录放行请求
                    return true;
                }
            }
        }

        //未登录则响应信息
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");//不设置这个就会乱码
        response.setStatus(401);
        response.getWriter().write("未通过登录认证，请在登录页进行登录");

        //不放行
        return false;
    }
}
